package com.ewanghuiju.app.ui.watermail.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.watermail.WaterHomeContract;
import com.ewanghuiju.app.model.bean.response.WaterGoodsClassifyBean;
import com.ewanghuiju.app.model.bean.response.WaterShopResponBean;
import com.ewanghuiju.app.presenter.watermail.WaterHomePresenter;
import com.ewanghuiju.app.ui.main.adapter.MyPagerAdapter;
import com.ewanghuiju.app.ui.watermail.fragment.WaterHomeFragment;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.gyf.immersionbar.ImmersionBar;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 水滴商城
 * @time 2020/2/19
 */
public class WaterHomeActivity extends BaseActivity<WaterHomePresenter> implements WaterHomeContract.View {

    @BindView(R.id.viewpage)
    ViewPager viewpage;
    @BindView(R.id.xtabLayout)
    XTabLayout xtabLayout;

    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;

    private List<Fragment> fragmentList;

    private List<String> strList;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_water_home;
    }

    @Override
    protected void initEventAndData() {
        tvTitleRight.setVisibility(View.VISIBLE);
        tvTitleRight.setText("兑换记录");
        tvTitleRight.setTextColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
        LoadingDialogUtils.show(mContext);
        mPresenter.getJfgoodscate();
    }

    private void initFragment(List<WaterGoodsClassifyBean> mList) {
        if (mList.size() > 4) {
            xtabLayout.setTabMode(XTabLayout.MODE_SCROLLABLE);
        } else {
            xtabLayout.setTabMode(XTabLayout.MODE_FIXED);
        }
        fragmentList = new ArrayList<>();
        strList = new ArrayList<>();
        for (int i = 0; i < mList.size(); i++) {
            strList.add(mList.get(i).getName());
            WaterHomeFragment waterHomeFragment = new WaterHomeFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(Constants.WATER_TYPE, mList.get(i).getId());
            waterHomeFragment.setArguments(bundle);
            fragmentList.add(waterHomeFragment);
        }
        viewpage.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), 0, fragmentList, strList));
        int currentPage = getIntent().getIntExtra(Constants.PINTUANORDER_TYPE, 0);
        viewpage.setCurrentItem(currentPage);

        xtabLayout.setupWithViewPager(viewpage);
    }


    @Override
    protected int getTitleIntText() {
        return R.string.water_shop_title;
    }

    @Override
    protected void intSmartRefreshLayout() {
//        super.intSmartRefreshLayout();
    }

    @OnClick({R.id.tv_title_right})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_title_right:
                //兑换记录
                new StartActivityUtil(mContext, WaterOrderListActivity.class).startActivity(true);
                break;
            default:
                break;
        }
    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(mToolbar)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    public void showContent(List<WaterShopResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
    }

    @Override
    public void showJfgoodscate(List<WaterGoodsClassifyBean> list) {
        //获取分类成功
        LoadingDialogUtils.dismissDialog_ios();
        List<WaterGoodsClassifyBean> myList = new ArrayList<>();
        WaterGoodsClassifyBean waterGoodsClassifyBean = new WaterGoodsClassifyBean();
        waterGoodsClassifyBean.setId(0);
        waterGoodsClassifyBean.setName("全部");
        myList.add(waterGoodsClassifyBean);
        if (list != null && list.size() > 0) {
            for (WaterGoodsClassifyBean bean : list) {
                myList.add(bean);
            }
        }
        initFragment(myList);
    }

    @Override
    public void stateError() {
        super.stateError();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
